//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Xml;
using System;

namespace OpenLS.Core.Serialization
{
    [CLSCompliant(true)]
    public class ExtensionElement : ExtensionNode
    {
        public ExtensionElement()
        {
            
        }
        private List<ExtensionAttribute> _attributes = new List<ExtensionAttribute>();

        public List<ExtensionAttribute> Attributes
        {
            get { return _attributes; }
            set { _attributes = value; }
        }
        private List<ExtensionNode> _children = new List<ExtensionNode>();

        public List<ExtensionNode> Children
        {
            get { return _children; }
            set { _children = value; }
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _namespaceUri;

        public string NamespaceUri
        {
            get { return _namespaceUri; }
            set { _namespaceUri = value; }
        }
        private string _prefix;
        private XmlNamespace ns;

        public string Prefix
        {
            get { return _prefix; }
            set { _prefix = value; }
        }


        #region IOfficeXmlSerializable Members

        internal override void ReadXmlCore(ReadContext context)
        {
            Name = context.Reader.LocalName;
            Prefix = context.Reader.Prefix;
            NamespaceUri = context.Reader.NamespaceURI;
            ns = new XmlNamespace(Prefix, NamespaceUri, false);
           //\\ Contract.Assert(NamespaceUri != @"http://schemas.openxmlformats.org/drawingml/2006/chart");
            Contract.Assert(NamespaceUri != @"urn:schemas-microsoft-com:vml");
            Contract.Assert(NamespaceUri != @"urn:schemas-microsoft-com:office:word");
#if DEBUG
            switch (Name)
            {
                case "ext":
                case "extLst":
                case "control":
                case "controls":
                case "controlPr":
                case "anchor":
                case "from":
                case "to":

                    break;
                default:
                    Contract.Assert(NamespaceUri != @"http://schemas.openxmlformats.org/spreadsheetml/2006/main" ||
                                    Name == "extLst");
                    break;
            }
#endif
            Contract.Assert(NamespaceUri != @"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            using (var c = context.Read(ns + Name)) //\\
            {
                if (context.Reader.HasAttributes)
                {
                    context.Reader.MoveToFirstAttribute();
                    while (true)
                    {
                        if (c.Reader.NamespaceURI != "http://www.w3.org/2000/xmlns/")
                        {
                            var a = new ExtensionAttribute
                                                       {
                                                           Name = c.Reader.LocalName,
                                                           Prefix = c.Reader.Prefix,
                                                           NamespaceUri = c.Reader.NamespaceURI,
                            Value = c.Reader.Value
                                                       };
                            Attributes.Add(a);
                        }
                        if (!context.Reader.MoveToNextAttribute())
                            break;

                    }
                }
                while (c.ReaderLocalName != null)
                {
                    if (c.Reader.NodeType == XmlNodeType.Text)
                    {
                        Children.Add(new ExtensionText(c.Reader.Value));
                        c.Reader.Read();
                    }
                    else
                        Children.Add(c.ReadElement<ExtensionElement>());
                }
            }

        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write(ns + Name))
            {
                foreach (ExtensionAttribute a in Attributes)
                {
                    c.SetString(a.Namespace + a.Name, a.Value);
                }
                foreach (ExtensionNode e in Children)
                    context.WriteElement(e);
            }
        }

        #endregion
    }

    }
